<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入医生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hospital:doctor:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hospital:doctor:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hospital:doctor:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hospital:doctor:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="doctorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医生姓名" align="center" prop="name" />
      <el-table-column label="院区" align="center" prop="ylArea.name" width="60"/>
      <el-table-column label="科室" :show-overflow-tooltip='true' align="center" prop="ylDepartment.name" >
        <template slot-scope="scope">
          <div v-if="scope.row.ylDepartment.superId!=0">{{scope.row.ylDepartment.superName}}—{{scope.row.ylDepartment.name}}</div>
          <div v-else>{{scope.row.ylDepartment.name}}</div>
        </template>
      </el-table-column>
<!--      <el-table-column label="职称" align="center" prop="ylTitleControl.titleName" />-->
      <el-table-column label="标签" align="center" :show-overflow-tooltip='true' prop="ylLabelArr" width="280">
        <template slot-scope="scope">
          <span v-for="(item,index) in scope.row.ylLabelArr" :key="index">
            <el-tag type="success" v-if="scope.row.ylLabelArr[index].length==2">{{item}}</el-tag>
            <el-tag type="warning" v-else-if="scope.row.ylLabelArr[index].length==3">{{item}}</el-tag>
            <el-tag type="primary" v-else-if="scope.row.ylLabelArr[index].length==4">{{item}}</el-tag>
            <el-tag type="danger" v-else>{{item}}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="医生简介" align="center" prop="description" :show-overflow-tooltip='true' width="280"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.state==1">正常</el-tag>
          <el-tag type="danger" v-else>停工</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hospital:doctor:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hospital:doctor:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入医生姓名" />
        </el-form-item>
        <el-form-item label="院区" prop="areaId">
<!--          <el-input v-model="form.areaId" placeholder="请输入院区" />-->
          <el-select v-model="form.areaId" @change="handleSelectArea" placeholder="请选择">
            <el-option
              v-for="item in areaOptions"
              :key="item.areaId"
              :label="item.name"
              :value="item.areaId"
              :disabled="item.state == 1"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="科室" prop="departmentId">
          <el-cascader
          v-model="form.departmentId"
          :options="options"
          :props="{ expandTrigger: 'hover' }"
          @change="handleDepartmentChange">
          </el-cascader>
        </el-form-item>

<!--        <el-form-item label="职称" prop="titleId">-->
<!--&lt;!&ndash;          <el-input v-model="form.titleId" placeholder="请输入职称" />&ndash;&gt;-->
<!--          <el-select v-model="form.titleId" @change="handleSelectTitle" placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in titleOptions"-->
<!--              :key="item.titleId"-->
<!--              :label="item.titleName"-->
<!--              :value="item.titleId"-->
<!--              :disabled="item.status == 1"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="标签" prop="ylLabelArr">
<!--          <el-input v-model="form.labelIds" placeholder="请输入标签" />-->
          <el-select v-model="form.ylLabelArr" @change="handleSelectLabel" multiple placeholder="请选择">
            <el-option
              v-for="item in labelOptions"
              :key="item.labelId"
              :label="item.name"
              :value="item.labelId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="挂号费" prop="registrationFee">
          <el-input v-model="form.registrationFee" placeholder="请输入挂号费" />
        </el-form-item>
        <el-form-item label="诊查费" prop="examinationFee">
          <el-input v-model="form.examinationFee" placeholder="请输入诊查费" />
        </el-form-item>
        <el-form-item label="医生简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="状态" prop="state">-->
<!--          <el-input v-model="form.state" placeholder="请输入状态" />-->
<!--        </el-form-item>-->
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio :label="item.value" :key="item.value" v-for="(item,index) in ifUseDoctor">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDoctor, getDoctor, delDoctor, addDoctor, updateDoctor, changeDoctorState } from "@/api/hospital/doctor";
import { allList , getDepartment2 } from "@/api/hospital/department";

export default {
  name: "Doctor",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医生信息表格数据
      doctorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否开启/停用医生
      ifUseDoctor: [
        { label: "正常", value: 1 },
        { label: "停工", value: 0 }
      ],
      // 标签选项
      labelOptions: [],
      // 院区选项
      areaOptions: [],
      // 职称选项
      titleOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        picture: null,
        areaId: null,
        departmentId: null,
        titleId: null,
        labelIds: null,
        registrationFee: null,
        examinationFee: null,
        description: null,
        state: null,
        revision: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        { key: 0, label: `状态`, visible: true },
      ],
      // 表单校验
      rules: {
        areaId: [
          { required: true, message: "院区不能为空", trigger: "blur" }
        ],
        departmentId: [
          { required: true, message: "科室不能为空", trigger: "blur" }
        ],
        // titleId: [
        //   { required: true, message: "职称不能为空", trigger: "blur" }
        // ],
        labelIds: [
          { required: true, message: "标签不能为空", trigger: "blur" }
        ],
      },
      //科室级联
      value: [],
      options: []
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.options = this.loadDept();
  },
  methods: {
    /** 查询医生信息列表 */
    getList() {
      this.loading = true;
      listDoctor(this.queryParams).then(response => {
        this.doctorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },



    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        doctorId: null,
        name: null,
        picture: null,
        areaId: null,
        departmentId: null,
        titleId: null,
        labelIds: null,
        registrationFee: null,
        examinationFee: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.doctorId)
      this.names = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //科室级联
    handleDepartmentChange(row) {
      this.form.departmentId = row[1];
    },
    // 用户状态修改
    handleStatusChange(row) {
      console.log(row);
      let text = row.state === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"医生吗？').then(function() {
        return changeDoctorState(row.doctorId, row.state);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.state = row.state === "0" ? "1" : "0";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加医生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const doctorId = row.doctorId || this.ids
      getDoctor(doctorId).then(response => {
        console.log(response.data);
        console.log(response.data.ylLabelList);
        this.form = response.data;
        this.labelOptions = response.data.ylLabelList;
        this.areaOptions = response.data.ylAreaList;
        this.titleOptions = response.data.ylTitleControlList;
        console.log(this.form);
        this.open = true;
        this.title = "修改医生信息";
      });
    },
    handleSelectArea(item) {
      console.log(item);

    },
    handleSelectTitle(item) {
      console.log(item);

    },
    //查询级联
    loadDept() {
      allList().then(response => {
        response.rows.forEach((item,index)=>{
            this.options.push({
              label:item.name,
              value:item.departmentId,
              children:[]
            })
            getDepartment2(item.departmentId).then(response1 => {
              response1.rows.forEach((item1,index1)=>{
                this.options[index].children.push({
                  label:item1.name,
                  value:item1.departmentId,
                })
              })
            });
        })
      });
      return this.options;
    },
    handleSelectLabel(item) {
      this.form.labelIds = "";
      var arr = "";
      for(let i = 0;i < item.length; i++){
        arr+=item[i] + ",";
      }
      if (arr.length > 0) {
        arr = arr.substr(0,arr.length - 1);
      }
      this.form.labelIds = arr;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.doctorId != null) {
            console.log(this.form);
            updateDoctor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDoctor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const docatorNames = row.name || this.names;
      const doctorIds = row.doctorId || this.ids;
      this.$modal.confirm('是否确认删除医生姓名为"' + docatorNames + '"的数据项？').then(function() {
        return delDoctor(doctorIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hospital/doctor/export', {
        ...this.queryParams
      }, `doctor_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
